package com.qf.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.AdminBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminBrandServiceImpl implements AdminBrandService {

    @Autowired
    private DtsBrandMapper dtsBrandMapper;

    @Override
    public IPage<DtsBrand> selectBrandList(Integer page, Integer limit, Integer id, String name, String sort, String order) {

        IPage<DtsBrand> iPage = new Page<>(page, limit);

        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", false);
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),order);
        wrapper.orderByDesc("`desc`".equalsIgnoreCase(order),order);
        wrapper.like(id != null, "id", id);
        wrapper.like(!StringUtils.isEmpty(name), "name", name);
        IPage<DtsBrand> result = dtsBrandMapper.selectPage(iPage, wrapper);
        return result;
    }

    @Override
    public DtsBrand insertBrand(DtsBrand dtsBrand) {

        dtsBrand.setDeleted(false);
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.insert(dtsBrand);

        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
        wrapper.select("id,`name`");
        wrapper.eq("deleted", false);
        List<DtsBrand> dtsBrands = dtsBrandMapper.selectList(wrapper);
        dtsBrand.setId(dtsBrands.get(0).getId());
        return dtsBrand;
    }

    @Override
    public void updateBrand(DtsBrand dtsBrand) {
        dtsBrandMapper.updateById(dtsBrand);
    }

    @Override
    public void deleteBrand(Integer id) {
        DtsBrand dtsBrand = new DtsBrand();
        dtsBrand.setId(id);
        dtsBrand.setDeleted(false);
        dtsBrandMapper.updateById(dtsBrand);
    }
}
